package tests;

import Common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;

import java.io.IOException;

public class LonginPage extends Utils {
    public static String url = "http://127.0.0.1:8080/login.html";


    public LonginPage() {
        super(url);
    }

    //检查页面是否加载成功
    public void loginPageRight() {
        //通过查看页面元素是否存在来检查页面加载成功与否
        driver.findElement(By.cssSelector("#body > div > h3"));
        //登陆输入框
        driver.findElement(By.cssSelector("#user"));
    }

    //检查登录功能---成功登录
    public void LoginSuc() throws IOException, InterruptedException {
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#user")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();
        //处理警告弹窗
        Thread.sleep(2000);
        Alert alert = driver.switchTo().alert();
        alert.accept();
        String currentUrl = driver.getCurrentUrl();
        assert "http://127.0.0.1:8080/list.html".equals(currentUrl);
//        getScreenShot(this.getClass().getName());
    }

    //检查登录功能---登录失败(用户名错误)
    public void LoginFail() throws InterruptedException, IOException {
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#user")).sendKeys("zhangsan111");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();
        //处理警告弹窗
        Thread.sleep(2000);
        Alert alert = driver.switchTo().alert();
        alert.accept();
        String currentUrl = driver.getCurrentUrl();
        assert "http://127.0.0.1:8080/login.html".equals(currentUrl);
//        getScreenShot(this.getClass().getName());
    }
}
